package com.hairline.hairlinemarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.hairlinemarket.bean.MarketCollect;
import com.hairline.hairlinemarket.bean.MarketCollectExample;
import com.hairline.hairlinemarket.bean.MarketGoods;
import com.hairline.hairlinemarket.bean.MarketUser;
import com.hairline.hairlinemarket.bean.vo.CollInfo;
import com.hairline.hairlinemarket.mapper.MarketCollectMapper;
import com.hairline.hairlinemarket.mapper.MarketGoodsMapper;
import com.hairline.hairlinemarket.service.CollectService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Author:杨小凯
 * Date:2023-04-23 20:40
 * Description:<描述>
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private MarketCollectMapper marketCollectMapper;

    @Autowired
    private MarketGoodsMapper marketGoodsMapper;
    @Override
    public void addCollect(Map map) {

        Integer type = (Integer) map.get("type");
        Integer valueId = (Integer) map.get("valueId");

        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        // 查询是否已经收藏
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.createCriteria().andUserIdEqualTo(user.getId())
                        .andTypeEqualTo(type.byteValue())
                                .andValueIdEqualTo(valueId);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        if (marketCollects!=null&&marketCollects.size()>0){
            MarketCollect marketCollect = marketCollects.get(0);
            Boolean deleted = marketCollect.getDeleted();
            MarketCollect record = new MarketCollect();
            record.setDeleted(!deleted);
            record.setUpdateTime(new Date());
            marketCollectMapper.updateByExampleSelective(record,marketCollectExample);
        }else {
            MarketCollect record = new MarketCollect();
            record.setType(type.byteValue());
            record.setValueId(valueId);
            record.setUserId(user.getId());
            record.setAddTime(new Date());
            record.setUpdateTime(new Date());
            int i = marketCollectMapper.insertSelective(record);
        }
    }

    @Override
    public PageInfo<CollInfo> showCollects(byte type, Integer page, Integer limit) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();

        // 获得用户收藏的所有数据
        Integer id = user.getId();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.createCriteria().andUserIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);

        if (marketCollects.size()==0||marketCollects==null){
            // 没有收藏
            return null;
        }

        List<CollInfo> collInfos = new ArrayList<>();
        // 根据valueId查询goods表
        for (MarketCollect marketCollect : marketCollects) {

            Integer valueId = marketCollect.getValueId();

            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(valueId);

            CollInfo collInfo = new CollInfo();
            collInfo.setType(marketCollect.getType());
            collInfo.setId(marketCollect.getId());
            collInfo.setValueId(valueId);
            collInfo.setName(marketGoods.getName());
            collInfo.setBrief(marketGoods.getBrief());
            collInfo.setPicUrl(marketGoods.getPicUrl());
            collInfo.setRetailPrice(marketGoods.getRetailPrice());

            collInfos.add(collInfo);
        }

        PageHelper.startPage(page, limit);
        PageInfo<CollInfo> pageInfo = new PageInfo<>(collInfos);
        return pageInfo;
    }

    @Override
    public int selectReslute(Integer valueId) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.createCriteria().andValueIdEqualTo(valueId).andUserIdEqualTo(user.getId()).andDeletedEqualTo(false);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        if (marketCollects.size()>0&&marketCollects!=null){
            return 1;
        }
        return 0;
    }
}
